<?php
use yii\helpers\Url;

$web = Url::base();
?>
<input type="hidden" id="inputManagerId" value="<?= $item['id'] ?>">
<div class="modal-body">
    <div class="form-horizontal">
        <div class="form-group">
            <label class="col-lg-3 col-sm-3 control-label">机构:</label>
            <div class="col-lg-9">
                <div id="inputInstitution"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-3 col-sm-3 control-label">类型：</label>
            <div class="btn-group col-lg-9">
                <button type="submit" class="btn btn-info" id="insManagerM" onclick="changeTypeM(1)" disabled="disabled">机构管理员
                </button>
                <button type="submit" class="btn btn-default" id="depManagerM" onclick="changeTypeM(2)">子机构(部门)管理员
                </button>
            </div>
        </div>
        <div class="form-group" id="subInsM">
            <label class="col-lg-3 col-sm-3 control-label">子机构(部门):</label>
            <div class="col-lg-9" id="detail_department_div">
                <div id="detail_inputDepartment"></div>
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-3 col-sm-3 control-label">姓名:</label>
            <div class="col-lg-9">
                <input type="text" class="form-control" id="inputName" value="<?= $item['name'] ?>"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-3 col-sm-3 control-label">账号:</label>
            <div class="col-lg-9">
                <input type="text" class="form-control" id="inputAccount" value="<?= $item['account'] ?>" readonly/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-3 col-sm-3 control-label">密码:</label>
            <div class="col-lg-9">
                <input type="password" class="form-control" id="inputPassword" disabled
                       value="<?= $item['password'] ?>"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-3 col-sm-3 control-label">手机号码:</label>
            <div class="col-lg-9">
                <input type="tel" class="form-control" id="inputPhone" value="<?= $item['phone'] ?>"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-3 col-sm-3 control-label">是否启用:</label>
            <div class="col-lg-9">
                <select class="form-control" id="inputIsEnabled">
                    <?php
                    if ($item['is_enabled'] == 1) {
                        ?>
                        <option value="1" selected>启用</option>
                        <option value="0">禁用</option>
                        <?php
                    } else {
                        ?>
                        <option value="1">启用</option>
                        <option value="0" selected>禁用</option>
                        <?php
                    }
                    ?>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label col-lg-3">备注:</label>
            <div class="col-lg-9">
                <textarea rows="4" class="form-control" id="inputRemark"><?= $item['remark'] ?></textarea>
            </div>
        </div>

        <div class="form-group">
            <div class="panel">
                <div class="panel-heading">
                    角色配置
                </div>
                <div class="panel-body" id="roleinsM">
                    <?php
                    $isSystemAdmin = \app\models\Permission::isSystemAdmin();
                    foreach ($roleList as $role) {
                            if (!$isSystemAdmin && $role['is_system'] == 1 || $role['is_department'] == 1) {
                                continue;
                            }
                        $isChecked = '';
                        if (in_array($role['id'], $item['role_list'])) {
                            $isChecked = 'checked';
                        }
                        ?>
                        <div class="square-blue form-inline icheck col-lg-4">
                            <div class="checkbox popovers" data-trigger="hover" data-placement="top"
                                 data-original-title="<?= $role['name'] ?>" data-content="<?= $role['description'] ?>">
                                <input type="checkbox" <?= $isChecked ?> name="role" value="<?= $role['id'] ?>">
                                <label><?= $role['name'] ?> </label>
                            </div>
                        </div>
                        <?php
                    }
                    ?>
                </div>
                <div class="panel-body" id="roledepM" style="display: none">
                    <?php
                    foreach ($roleList as $role) {
                        if ($role['is_department'] == 1) {
                        $isChecked = '';
                        if (in_array($role['id'], $item['role_list'])) {
                            $isChecked = 'checked';
                        }
                        ?>
                        <div class="square-blue form-inline icheck col-lg-4">
                            <div class="checkbox popovers" data-trigger="hover" data-placement="top"
                                 data-original-title="<?= $role['name'] ?>" data-content="<?= $role['description'] ?>">
                                <input type="checkbox" <?= $isChecked ?> name="role" value="<?= $role['id'] ?>">
                                <label><?= $role['name'] ?> </label>
                            </div>
                        </div>
                        <?php
                    }}
                    ?>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal-footer">
    <button class="btn btn-default" data-dismiss="modal">取消</button>
    <button class="btn btn-primary" id="btnModifyOk">保存修改</button>
</div>

<script type="application/javascript">

    var institutionSelect;
    $(function () {
        $('.popovers').popover();

        if ("<?=$department_id?>" != '') {
            $("#subInsM").css("display", "block");
            $("#depManagerM").attr("disabled", true).removeClass('btn-default').addClass('btn-info');
            $("#insManagerM").attr("disabled", false).removeClass('btn-info').addClass('btn-default');
            $("#roledepM").css("display", "block");
            $("#roleinsM").css("display", "none");
        } else {
            $("#subInsM").css("display", "none");
            $("#insManagerM").attr("disabled", true).removeClass('btn-default').addClass('btn-info');
            $("#depManagerM").attr("disabled", false).removeClass('btn-info').addClass('btn-default');
            $("#roledepM").css("display", "none");
            $("#roleinsM").css("display", "block");
        }
        ;

        var rootItem = {};
        rootItem.name = '系统';
        rootItem.code = '0';
        rootItem.children = <?= json_encode($institutions) ?>;
        var institutionSelectSource = [];
        institutionSelectSource.push(rootItem);
        institutionSelect = $('#inputInstitution').treeAutocomplete({
            defaultText: '---- All ----',
            source: institutionSelectSource
        });
        <?php
        if($item['institution_code'] != ''){
        ?>
        institutionSelect.setValue('<?=$item['institution_code']?>');
        institutionSelect.setDisabled(true);
        <?php
        }
        ?>

        $('.square-blue input').iCheck({
            checkboxClass: 'icheckbox_square-blue',
            radioClass: 'iradio_square-blue',
            increaseArea: '20%' // optional
        });

        //绑定子机构
        $("#detail_department_div").show();
        var departments = <?= json_encode($departments) ?>;
        var subdefaultText="--选择子机构(部门)--";
        if(departments==''){
            subdefaultText="无子机构(部门)，请到[机构管理]-[子机构(部门)设置]中维护";
        }
        var detail_inputDepartment = $('#detail_inputDepartment').treeAutocomplete({
            defaultText: subdefaultText,
            isShowCode: false,
            source: departments
        }).setValue("<?=$department_id?>");

        $('#btnModifyOk').click(function () {
            var data = {};
            if ($("#depManagerM").prop('disabled') == true && $('#detail_inputDepartment').val() == '') {
                alert("请选择子机构(部门)");
                return;
            }
            if ($("#depManagerM").prop('disabled') == true) {
                data.department_id = $('#detail_inputDepartment').val();
            } else {
                data.department_id = '';
            }

            data.id = $('#inputManagerId').val();
            data.institution_code = institutionSelect.getValue();

            data.name = $('#inputName').val();
            data.account = $('#inputAccount').val();
            data.phone = $('#inputPhone').val();
            data.is_enabled = $('#inputIsEnabled').val();
            data.remark = $('#inputRemark').val();
            data.role_list = [];
            $("input:checkbox[name=role]:checked").each(function () {
                data.role_list.push($(this).val());
            });
            requestJson('modify-manager-handler', data, true, function (responseData) {
                if (responseData.status == 0) {
                    window.location.reload();
                } else {
                    alert(responseData.message);
                }
            });
        });

    });

    function changeTypeM(i) {
        if(i==2){
            $("#subInsM").css("display", "block");
            $("#insManagerM").attr("disabled", false).removeClass('btn-info').addClass('btn-default');
            $("#depManagerM").attr("disabled", true).removeClass('btn-default').addClass('btn-info');
            $("#roledepM").css("display", "block");
            $("#roleinsM").css("display", "none");
        } else {
            $("#subInsM").css("display", "none");
            $("#insManagerM").attr("disabled", true).removeClass('btn-default').addClass('btn-info');
            $("#depManagerM").attr("disabled", false).removeClass('btn-info').addClass('btn-default');
            $("#roledepM").css("display", "none");
            $("#roleinsM").css("display", "block");
        }
        $('input').iCheck('uncheck');

    }


</script>
